﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HCommon;
using eStock.hControl;

namespace eStock
{
    public partial class frmItems : hForm
    {
        public frmItems()
        {
            InitializeComponent();
            //this.SaveButton = btnSave;
            //this.QueryButton = btnSearch;
        }

        private void frmItems_Load(object sender, EventArgs e)
        {
            LoadGrid();
        }

        private void LoadGrid()
        {
            try
            {
                grdListItem.DataSource = Dao.Get_Table("EXEC spItem_Q N'" + txtItemCd.Text + "',N'" + txtItemName.Text + "',N'" + cboUnit.SelectedValue.ToString() + "',N'" + cboOrigin.SelectedValue.ToString() + "'");
            }
            catch (Exception ex)
            {
                Common.GhiLog("LoadGrid", ex);
                Dialog.ShowMess(Dialog.mType.err, ex.Message);
            }
        }

        private void SaveGrid()
        {
            int okCnt = 0;
            foreach (DataGridViewRow row in grdListItem.Rows)
            {
                try
                {
                    //if (Convert.ToString(row.Cells["Tag"].Value).Trim().Length == 0)
                    //{
                    //    continue;
                    //}

                    //okCnt += tblItem.Exec(Convert.ToString(row.Cells["Tag"].Value), Convert.ToString(row.Cells["ID"].Value), Convert.ToString(row.Cells["ItemCd"].Value), Convert.ToString(row.Cells["ItemName"].Value), Convert.ToString(row.Cells["UnitCd"].Value));
                }
                catch (Exception ex)
                {
                    Common.GhiLog("LoadGrid", ex);
                    Dialog.ShowMess(Dialog.mType.err, ex.Message);
                }
            }
            Dialog.ShowMess(Dialog.mType.ok, "Đã lưu thành công " + okCnt + " bản ghi!");
            int rowIndex = grdListItem.CurrentRow.Index;
            LoadGrid();
            grdListItem.FirstDisplayedScrollingRowIndex = rowIndex;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            SaveGrid();
        }

        private void btnQry_Click(object sender, EventArgs e)
        {
            LoadGrid();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            frmItems_Add ItemAdd = new frmItems_Add();
            ItemAdd.FormMode = Mode.Insert;
            ItemAdd.ShowDialog();
            if (ItemAdd != null)
            {
                LoadGrid();
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void grdListItem_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (grdListItem.Rows.Count == 0 || e.RowIndex < 0)
                {
                    return;
                }
                frmItems_Add ItemAdd = new frmItems_Add();
                ItemAdd.UpdateKey = grdListItem.Rows[e.RowIndex].Cells["ItemCd"].Value.ToString();
                ItemAdd.FormMode = Mode.Update;
                ItemAdd.ShowDialog();
                if (ItemAdd != null)
                {
                    LoadGrid();
                }
            }
            catch (Exception ex)
            {
                Common.GhiLog("grdListItem_CellDoubleClick", ex);
                Dialog.ShowMess(Dialog.mType.err, ex.Message);
            }
        }

        private void grdListItem_hDelete(object sender, EventArgs e)
        {
            try
            {
                int delCnt = 0;
                foreach (DataGridViewRow row in grdListItem.SelectedRows)
                {
                    if (tblItem.Delete(row.Cells["ItemCd"].Value.ToString()) > 0)
                    {
                        grdListItem.Rows.Remove(row);
                        delCnt++;
                    }
                }
                Dialog.ShowMess(Dialog.mType.ok, "Đã xóa thành công " + delCnt + " bản ghi.");
            }
            catch (Exception ex)
            {
                Common.GhiLog("grdListItem_hDelete", ex);
                Dialog.ShowMess(Dialog.mType.err, ex.Message);
            }
        }

    }
}
